package org.graduates.webapp.actions.course;

import java.util.Date;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.graduates.webapp.actions.BaseAction;
import org.projects.graduates.domain.ContentStatus;
import org.projects.graduates.domain.HomeworkAssignments;
import org.projects.graduates.domain.HomeworkHandin;

@Action(results = { @Result(name = "input", type = "json"),
		@Result(name = "success", type = "json", params = { "includeProperties", "haid,success,handin,handin.id" }) })
public class HomeworkReceiveAction extends BaseAction {
	private static final long serialVersionUID = 8481139941173566959L;
	private long haid = 0L;
	private HomeworkHandin handin;

	private boolean success = false;

	public String execute() {
		if (0L == haid) {
			return INPUT;
		}

		HomeworkAssignments ass = HomeworkAssignments.get(HomeworkAssignments.class, haid);

		if (null == ass) {
			return INPUT;
		}
		
		handin = new HomeworkHandin(ass, new Date(), ContentStatus.DRAFT);

		this.gradApplication.save(handin);

		success = true;

		return SUCCESS;
	}

	public HomeworkHandin getHandin() {
		return handin;
	}

	public long getHaid() {
		return haid;
	}

	public void setHaid(long haid) {
		this.haid = haid;
	}

	public boolean isSuccess() {
		return success;
	}

}
